<template>
  <el-card>
    <el-form :inline="true" :model="form">
      <el-form-item>
        <el-input v-model="form.order_no" placeholder="订单号" clearable />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.review_status"
          placeholder="审核状态"
          clearable
          class="w-28"
        >
          <el-option
            v-for="item in reviewStatusList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.refund_status"
          placeholder="退款状态"
          clearable
          class="w-28"
        >
          <el-option
            v-for="item in refundStatusList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="form.timeData"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="截止日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">
          <span class="px-5">查询</span></el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border v-loading="loading">
      <el-table-column prop="order_no" label="订单号" align="center" />
      <el-table-column prop="amount" label="退款金额" align="center" />
      <!-- <el-table-column prop="recharge_amount" label="充值金额" align="center" /> -->
      <el-table-column prop="refund_status" label="退款状态" align="center">
        <template #default="{ row }">
          {{
            row.refund_status === 0
              ? "退款中"
              : row.refund_status === 1
              ? "退款成功"
              : "退款失败"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="review_status" label="审核状态" align="center">
        <template #default="{ row }">
          {{
            row.review_status === 0
              ? "审核中"
              : row.review_status === 1
              ? "成功"
              : "失败"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="申请时间" align="center" :formatter="formatterTime" />
      <el-table-column label="操作" align="center" fixed="right" width="140">
        <template #default="scope">
          <span
            class="text-[#e54c5f] cursor-pointer"
            @click="handleDetails(scope.row)"
            >查看订单</span
          >
        </template>
      </el-table-column>
      <!-- <el-table-column prop="refund_status" label="退款状态" align="center" />
      <el-table-column prop="sys_review_status" label="审核状态" align="center" /> -->
      <!-- <el-table-column
        prop="create_time"
        label="申请时间"
        align="center"
        :formatter="formatterTime"
      />
      <el-table-column
        prop="finish_time"
        label="完成时间"
        align="center"
        :formatter="formatterTime"
      /> -->
    </el-table>
    <div class="mt-5 flex justify-center">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="pageable.total"
        :default-current-page="pageable.pageNum"
        :default-page-size="pageable.pageSize"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
</template>
<script setup>
import { ref, reactive } from "vue";
import { refundListApi } from "@/api/account/index";
import { timestamp,formatterTime } from "@/utils/utils";
import { useRouter } from "vue-router";
const router = useRouter();
const loading = ref(false);
const form = reactive({
  order_no: "",
  review_status: "",
  refund_status: "",
  timeData: [],
});
const reviewStatusList = ref([]);
const refundStatusList = ref([]);
// 分页
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 20,
  total: 0,
});
// 表格数据
const tableData = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    order_no: form.order_no,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    review_status: form.review_status,
    refund_status: form.refund_status,
  };
};
const onSubmit = () => {
  getTbaleData();
};
//查看订单
const handleDetails = ({ ...row }) => {
  router.replace({
    path: "/account/refund/details",
    query: { order_no: row.order_no },
  });
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    // 清空之前的数据
    tableData.value = [];
    // 获取数据

    const res = await refundListApi(param());
    // console.log(res);
    loading.value = false;
    tableData.value = res.list;
    pageable.total = res.page.total_count;
    reviewStatusList.value = res.extra.filter_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
    refundStatusList.value = res.extra.filter_refund_status.map((item) => {
      return { value: item.value, label: item.name };
    });
  } catch (error) {
    loading.value = false;
  }
};
getTbaleData();
</script>
